package com.itheima.udp3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class Server {

    public static void main(String[] args) throws Exception {
        //创建组播接收对象
        MulticastSocket ms = new MulticastSocket(8888);
        //对象添加到组中
        ms.joinGroup(InetAddress.getByName("224.0.1.0"));
        while (true) {
            DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
            ms.receive(dp);
            byte[] data = dp.getData();
            int length = dp.getLength();
            InetAddress address = dp.getAddress();
            System.out.println("来自"+address.getHostName()+"ip是"+address.getHostAddress()+"发送了:"+
                    new String(data,0,length));
        }


    }
}
